import numpy as np
import scipy.io as sio
import matplotlib.pyplot as plt
from sklearn.svm import SVC

data = sio.loadmat('ex6data2.mat')
X, y = data['X'], data['y']

def plot_data():
    plt.scatter(X[:, 0], X[:, 1], c=y.flatten(), cmap='jet')
    plt.xlabel('x1')
    plt.ylabel('y1')
    plt.show()

plot_data()

svc1 = SVC(C=1, kernel='rbf', gamma=50)
svc1.fit(X, y.flatten())
print(svc1.score(X, y.flatten()))
# 得分：0.8088064889918888  gamma=1
# 得分：0.9895712630359212  gamma=50


# 绘制决策边界
def plot_boundary(model):  # 传入训练好的模型
    x_min, x_max = 0, 1
    y_min, y_max = 0.4, 1
    xx, yy = np.meshgrid(np.linspace(x_min, x_max, 500),
                         np.linspace(y_min, y_max, 500))
    z = model.predict(np.c_[xx.flatten(), yy.flatten()])
    zz = z.reshape(xx.shape)
    plt.contour(xx, yy, zz)

plot_boundary(svc1)
plot_data()

